<?php

/**
 * User form.
 *
 * @package    maximous
 * @subpackage form
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class UserForm extends BaseUserForm
{

    public function configure()
    {
        $i18n      = $this->getOption('i18n');
        $noCaptcha = $this->getOption('noCaptcha', false);
        /*
         * set statues widget
         */
        $this->setWidget('statues', new sfWidgetFormChoice(array('choices'  => User::$statues, 'expanded' => true)));
        $this->setValidator('statues', new sfValidatorChoice(array('required' => false, 'choices'  => array_keys(User::$statues))));

        /*
         * set photo upload widget
         */
        $this->setWidget('photo', new sfWidgetFormInputFileEditable(array('file_src'  => false,
                    'is_image'  => true,
                    'edit_mode' => !$this->isNew(),
                    'template'  => ' %input% <br />' . $i18n->__('available types jpg ,png ,jpeg , gif') . '<br />%delete% %delete_label%'
                )));
        $this->setValidator('photo', new sfValidatorFile(array(
                    'mime_types' => sfConfig::get('app_images_mime_types'),
                    'required'   => FALSE,
                    'path'       => sfConfig::get('sf_root_dir') . '/' . sfConfig::get('app_dir_user_images'))));

        /*
         * add captcha for register
         */
        if (!$noCaptcha)
        {
            $this->setWidget('captcha', new sfWidgetCaptchaGD());
            $this->setValidator('captcha', new sfCaptchaGDValidator(array('length' => 4), array('invalid'  => $i18n->__("Incorrect captcha"), 'required' => 'Required.')));
        }
        /*
         * set email validator
         */
        $this->setValidator('email', new sfValidatorRegex(array('pattern'    => '/^[a-z][a-z0-9_.-]*@[a-z0-9.-]+[.][a-z]{2,}$/ii', 'min_length' => 5, 'max_length' => 255, 'required'   => true), array('invalid' => $i18n->__('Please enter a valid email!'))));
        /*
         * configure password
         */
        $this->setWidget('password', new sfWidgetFormInputPassword());
        $this->setWidget('re_password', new sfWidgetFormInputPassword());
        $this->setValidator('password', new sfValidatorString(array('min_length' => 6, 'required'   => $this->isNew()), array('min_length' => $i18n->__('the password is too short'))));
        $this->setValidator('re_password', new sfValidatorString(array('min_length' => 6, 'required'   => $this->isNew()), array('min_length' => $i18n->__('the password is too short'))));
        $this->validatorSchema->setOption('allow_extra_fields', true);
        $this->validatorSchema->setOption('filter_extra_fields', false);
        $this->addFormatter('CustomFormatter');

        $this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 're_password'));
    }

}
